﻿Public Class IssueTypes

    Private Sub btnClose_Click(sender As System.Object, e As System.EventArgs) Handles btnClose.Click
        '
        ' Close the Issue Type Form
        '
        Me.Close()
    End Sub

    Private Sub btnNewIssue_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNewIssue.Click
        ' Prompt the user for the new Priority Type
        Dim desc As String = InputBox("Please enter a new Issue Type Description: ", "EZ Ticket - New Issue Type", "Enter Description here..")

        ' Ensure the user enters a priority type, add the priority to the db and refresh the datagrid to show the results
        If desc <> "" Then
            clsUIBase.AddIssueType(desc)
            RefreshDataGrid()
        Else
            MessageBox.Show("You cannot enter a blank description", "EZ Ticket - Input Entry Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
    End Sub

    Private Sub IssueTypes_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        RefreshDataGrid()
    End Sub

    Private Sub RefreshDataGrid()
        ' Populate the Datagrid from values in the db
        dgIssueTypes.AutoGenerateColumns = False
        dgIssueTypes.DataSource = clsUIBase.RetrieveIssueTypes()
        dgIssueTypes.AutoResizeColumns()
    End Sub

    Private Sub dgIssueTypes_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dgIssueTypes.SelectionChanged
        ' Ensure a value is selected in the datagrid before the delete button will function
        If dgIssueTypes.SelectedCells.Count > 0 Then
            Me.btnDeleteIssueType.Enabled = True
        Else
            Me.btnDeleteIssueType.Enabled = False
        End If
    End Sub

    Private Sub btnDeleteIssueType_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDeleteIssueType.Click
        ' Ensure a row is selected
        If dgIssueTypes.SelectedCells.Count > 0 Then
            ' Determine the id of the currently selected priority
            Dim id As Integer = dgIssueTypes.Rows(dgIssueTypes.CurrentRow.Index).Cells(0).Value()

            ' if the id is not null, delete it from the db and refresh the datagrid to show the results
            If id <> Nothing Then
                clsUIBase.DeleteIssueType(id)
                RefreshDataGrid()
            End If
        End If
    End Sub
End Class